/************************************************************************************************
 * test examples of 100 interesting program in C
 * test 078.c
 * Approximation of PI
 ***********************************************************************************************/

#include <stdio.h>
#include <math.h>

/*
 * polygon approximating:
 * assuming edge-length of polygon is 2b, count of edge is i
 * when i comes to 2i, b' = sqrt(b^2 + (1-sqrt(1-b^2))^2)/2 = sqrt(2-2*sqrt(1-b^2))/2
 * start from 2b = r(6-edge polygon)
 */

int main()
{
    int i = 6, j = 0;
    double b = 0.5;
    double circle = 0.0;
    for (; j < 10; j++) // 10 times approximating
    {
        i *= 2;
        b = sqrt(2 - 2*sqrt(1-b*b))/2;
    }

    circle = 2*b*i;
    printf("pi = %f\n", circle/2);
}

